<template>
  <div>

    <div class="app_card mb-2 pt-4  pb-4">
      <!-- 权限 -->
      <!-- v-if="authStore.hasCode('SysConfigs.save')" -->
      <div class="d-flex justify-content-between">
        <div class="flex-grow-1">
          <el-radio-group class="radio_button" v-model="radioFloor" @change="floorChange">
            <el-radio-button label="-1">全部</el-radio-button>
            <el-radio-button label="-2">上课中</el-radio-button>
            <el-radio-button label="-3">故障教室</el-radio-button>
          </el-radio-group>

        </div>
        <div class="d-flex">
          <el-button type="primary" @click="onAdd()">新增设备 </el-button>
          <el-button type="primary" plain @click="onSearch()">导入</el-button>
          <el-button type="primary" plain @click="onSearch()">导出</el-button>
        </div>
      </div>
    </div>

    <div class="app_card pt-4  pb-5">
      <el-table stripe size="large" :data="dataOfTable" height="calc(100vh - 300px)">
        <el-table-column label="设备ID" prop="id" align="center" minWidth="100">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="大楼" prop="building" align="center" show-overflow-tooltip minWidth="100">
          <template #default="scope">{{ scope.row.building }}</template>
        </el-table-column>
        <el-table-column label="楼层" prop="floor" align="center" show-overflow-tooltip minWidth="100">
          <template #default="scope">{{ scope.row.floor }}</template>
        </el-table-column>
        <el-table-column label="教室" prop="room" align="center">
          <template #default="scope">{{ scope.row.room }}</template>
        </el-table-column>
        <el-table-column label="设备名称" prop="name" align="center" show-overflow-tooltip minWidth="100">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="设备品牌" prop="brand" align="center" show-overflow-tooltip minWidth="100">
          <template #default="scope">{{ scope.row.brand }}</template>
        </el-table-column>
        <el-table-column label="设备型号" prop="model" align="center" show-overflow-tooltip minWidth="100">
          <template #default="scope">{{ scope.row.model }}</template>
        </el-table-column>
        <el-table-column label="序号" prop="group" align="center" show-overflow-tooltip minWidth="100">
          <template #default="scope">
            {{findValue(grouOption,scope.row.group)}}
          </template>
        </el-table-column>
        <el-table-column label="购买时间" prop="purchasingDate" align="center" show-overflow-tooltip minWidth="100">
          <template #default="scope">
            {{scope.row.purchasingDate.split(" ")[0]}}
          </template>
        </el-table-column>
        <el-table-column label="寿命周期" prop="serviceLife" align="center" show-overflow-tooltip minWidth="100">
          <template #default="scope">{{ scope.row.serviceLife }}</template>
        </el-table-column>
        <el-table-column label="使用时长" prop="purchasingDate" width="180">
          <template #default="scope">
            <el-progress :stroke-width="18" :percentage="fontDatas(scope.row.purchasingDate)"></el-progress>
          </template>
        </el-table-column>
        <el-table-column label="设备类型" prop="type" align="center" show-overflow-tooltip minWidth="100">
          <template #default="scope">
            <template v-if="scope.row.type">
              {{findValue(typeOption,scope.row.type)}}</template>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="160" align="center">
          <template #default="scope">
            <el-button link type="primary" @click="onEditRow(scope.row)">编辑</el-button>
            <el-button link type="primary" @click="onDeleteRow(scope.row)">删除</el-button>
            <el-button link type="primary" @click="goClassroomDetail(scope.row)">跳转</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt-2">
        <el-pagination background v-model:currentPage="queryParam.page" v-model:page-size="queryParam.pageSize" :page-sizes="paginationOption.pageSizes" :background="false" :layout="paginationOption.layout" :total="paginationOption.total" @size-change="handleChangeSize" @current-change="handleChangeCurrent" />
      </div>
    </div>

    <EditDialog ref="editDialogRef" @success="loadData()"></EditDialog>
  </div>
</template>

<script setup>
/**
 * 教室管理
 */
import { useRouter, useRoute } from "vue-router";

import { ref, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
import useAuthStore from '@/stores/useAuthStore';
import { typeList, groupList } from '@/apis/model/EquipmentModel';
import { findValue } from '@/utils/EsCommon';

//组件
import EditDialog from './EditDialog.vue';
// ======================================================
//                      变量声明
// ======================================================
const router = useRouter();
const authStore = useAuthStore();

// 组件引用
const editDialogRef = ref(null);

const typeOption = ref(typeList);
const grouOption = ref(groupList);
// 表格数据
const dataOfTable = ref([]);
// 表格查询参数
const queryParam = ref({
  q: '',
  search: '',
  orderby: '',
  group_id: 0,
  page: 1,
  pageSize: 10,
})
// 分页栏配置数据
const paginationOption = ref({
  layout: 'total, sizes, prev, pager, next, jumper',
  pageSizes: [10, 20, 50, 100, 500, 1000],
  total: 0,
})
const radioFloor = ref('-1');

//
// ======================================================
//                      处理事件
// ======================================================

const floorChange = () => {
  if (radioFloor.value == "-2") {//上课
  } else if (radioFloor.value == "-3") {//故障教室
  } else if (radioFloor.value == "-1") {//全部
  } else {

  }
}
const fontDatas = (row) => {
  //console.log(row)
  var nowTime = row;
  var thisTime = nowTime;
  thisTime = thisTime.replace(/-/g, "/");
  var time = new Date(thisTime);
  time = time.getTime();
  var time_stamp = Date.parse(new Date());
  return parseInt(
    ((time_stamp - time) / 1000 / 60 / 60 / 24 / 30 / 36) * 100
  );
}


// 点击新增按钮
const onAdd = (row = null) => {
  if (row) {
    editDialogRef.value.show(0, "新增" + row.title, row);
    return;
  }
  editDialogRef.value.show(0, "新增");
}


// 点击快速搜索按钮
const onSearch = (param) => {
  queryParam.value.page = 1;
  queryParam.value.q = param;

  loadData();
}

// 点击行内编辑按钮
const onEditRow = (row) => {
  editDialogRef.value.show(row.id, "编辑", row);
}

// 点击行内删除按钮
const onDeleteRow = (row) => {
  ElMessageBox.confirm("确定要删除该记录吗？", "警告").then(() => {
    EsAjax.delete(ApiUrls.articleCategoryDelete, { data: { id: row.id } })
      .then(res => {
        if (res.data.code !== 200) {
          ElMessage.warning(res.data.msg);
          return;
        }
        loadData();
      })
      .catch(err => { ElMessage.error("网络错误：", err.message) });
  }).catch(() => { });
  console.log(row)
}
// 点击跳转
const goClassroomDetail = (row) => {
  router.push({ path: '/classroom/detail', query: { id: row.id } })

}


// 处理分页尺寸
const handleChangeSize = (val) => {
  console.log(`${val} items per page`);
  queryParam.value.page = 1;
  queryParam.value.pageSize = val;
  loadData();
}

// 处理页码
const handleChangeCurrent = (val) => {
  console.log(`current page: ${val}`)
  queryParam.value.page = val;
  loadData();
}

// ======================================================
//                      处理事件
// ======================================================

const loadData = () => {
  EsAjax.post(ApiUrls.equipmentDatagrid, queryParam.value).then(res => {
    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    console.log(res.data.data)
    dataOfTable.value = res.data.data.data;
    paginationOption.value.total = res.data.data.total;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}


onMounted(() => {
  loadData();
})

</script>
<style lang="less" scoped>
:deep(.el-progress-bar__inner) {
  background-color: #ffbb00;
}
</style>

